local skel = fk.CreateSkill {
  name = "rmt__chouren",
  dynamic_desc = function (self, player, lang)
    local skillName = self.name
    local mark = player:getMark(skillName.."_event")
    if type(mark) == "table" then
      if #mark == 0 then return skillName.."_none" end
      local arg = ""
      for _, v in ipairs(mark) do
        arg = arg..Fk:translate("rmt__zhaoevent_"..v, lang).."，"
      end
      return skillName.."_dyn:"..arg
    end
  end,
}

Fk:loadTranslationTable{
  ["rmt__chouren"] = "筹刃",
  [":rmt__chouren"] = "你每回合首次造成伤害后，你抵消牌时，有角色死亡后，你可以调整手牌数至4，若摸牌数大于X，将当前发动时机移动至“直诛”。",

  [":rmt__chouren_dyn"] = "{1}你可以调整手牌数至4，若摸牌数大于X，将当前发动时机移动至“直诛”。",
  [":rmt__chouren_none"] = "无合法时机。你可以调整手牌数至4，若摸牌数大于X，将当前发动时机移动至“直诛”。",

  ["#rmt__chouren-discard"] = "筹刃：你可以弃置 %arg 张手牌，然后无事发生",
  ["#rmt__chouren-draw"] = "筹刃：你可以摸 %arg 张牌",
  ["#rmt__chouren-drawtrans"] = "筹刃：你可以摸 %arg 张牌，且将时机（%arg2）移动至“直诛”",

  ["$rmt__chouren1"] = "冤魂扰乱、仇雠安睡，莫失莫忘！",
  ["$rmt__chouren2"] = "枕干怀刃，耻恨未涤，虽生犹死。",
}

skel:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, self.name.."_event", {"damage", "cancel", "death"})
end)

local zhaoeEventMap = {[fk.Damage] = "damage", [fk.CardEffecting] = "cancel", [fk.Deathed] = "death", [fk.EventPhaseStart] = "start"}

--- 能否触发此时机
---@param player ServerPlayer
local function can_trigger(skill, event, target, player, data)
  if not player:hasSkill(skel.name) then return false end
  if player:getHandcardNum() == 4 then return false end
  local mark = player:getTableMark(skel.name.."_event")
  if not table.contains(mark, zhaoeEventMap[event.class]) then return false end
  if event == fk.EventPhaseStart then
    return player.phase == Player.Start and target == player
  elseif event == fk.Damage then
    if target == player then
      local firstDamage = player.room.logic:getActualDamageEvents(1, function(e) return e.data.from == player end)[1]
      return firstDamage and firstDamage.data == data
    end
  elseif event == fk.CardEffecting then
    -- 抵消牌时，注意，不能使用 CardEffectCancelledOut，fk.CardEffectFinished
    -- data.responseToEvent and data.responseToEvent.card and data.responseToEvent.isCancellOut
    return data.from == player and data.toCard
  elseif event == fk.Deathed then
    return true
  end
  return false
end

local on_cost = function (self, event, target, player, data)
  local prompt = ""
  local x = player:getHandcardNum() - 4
  if x > 0 then
    prompt = "#rmt__chouren-discard:::"..x
  else
    x = -x
    local num = #player:getTableMark("rmt__zhizhu_event")
    if x > num then
      local e = zhaoeEventMap[event.class]
      e = e and ("rmt__zhaoevent_"..e) or "?"
      prompt = "#rmt__chouren-drawtrans:::"..x..":"..e
    else
      prompt = "#rmt__chouren-draw:::"..x
    end
  end
  return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = prompt })
end

---@param player ServerPlayer
local on_use = function (self, event, target, player, data)
  local room = player.room
  local x = player:getHandcardNum() - 4
  if x > 0 then
    room:askToDiscard(player, {
      min_num = x, max_num = x, skill_name = skel.name, cancelable = false, include_equip = false,
    })
  else
    local draw = player:drawCards(-x, self.name)
    if player.dead then return end
    local num = #player:getTableMark("rmt__zhizhu_event")
    if draw and #draw > num then
      local e = zhaoeEventMap[event.class]
      if e then
        local mark = player:getTableMark("rmt__chouren_event")
        table.removeOne(mark, e)
        room:setPlayerMark(player, "rmt__chouren_event", mark) -- 别用 removeTableMark
        room:addTableMarkIfNeed(player, "rmt__zhizhu_event", e)
      end
    end
  end
end

--- 四个时机一模一样

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.Damage, {
  anim_type = "drawcard",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.CardEffecting, {
  anim_type = "drawcard",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.Deathed, {
  anim_type = "drawcard",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})


return skel
